package com.zj.album.graphy.views;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.zj.album.R;
import com.zj.album.graphy.PhotoFileHelper;
import com.zj.album.graphy.adapter.Pop_ChooseFileAdapter;
import com.zj.album.interfaces.PhotoEvent;
import com.zj.album.utils.DisplayUtils;

import java.lang.ref.WeakReference;

/**
 * Created by zhaojie on 2017/10/25.
 */

public class ChooseFilePop extends PopupWindow {

    private WeakReference<View> contain;
    private View rootView;
    private WeakReference<Context> context;
    private Pop_ChooseFileAdapter adapter;
    private String curFileName;
    private ListView lvContent;
    private SmallLoadingView slvLoading;
    private PhotoEvent pEvent;

    public ChooseFilePop(String curFileName, Context context, View v) {
        this.contain = new WeakReference(v);
        this.curFileName = curFileName;
        this.context = new WeakReference(context);
        initView();
        initData();
    }

    private void initView() {
        rootView = LayoutInflater.from(context.get()).inflate(R.layout.pop_choosefile, null, false);
        setContentView(rootView);
        setAnimationStyle(R.style.photo_AnimationFade);
        lvContent = rootView.findViewById(R.id.pop_lvFile);
        slvLoading = rootView.findViewById(R.id.pop_slvLoading);
        int[] size = DisplayUtils.getDefaultDisplayMetrics(context.get());
        setWidth(size[0]);
        setHeight(size[1] - DisplayUtils.dip2px(context.get(), 50));
        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                dismiss();
                return false;
            }
        });
    }

    private void initData() {
        slvLoading.setCallInterface(new SmallLoadingView.CallRefresh() {
            @Override
            public void onCallRefresh() {

            }
        });
        adapter = new Pop_ChooseFileAdapter(context.get(), new IRecyclerAdapter.OnItemCLickListener() {
            @Override
            public void onItemClick(int postion, View view) {
                adapter.notifyDataSetChanged();
                slvLoading.loading("");
                PhotoFileHelper.getInstance(curFileName).setCurFileInfo(adapter.getItem(postion), new PhotoEvent() {
                    @Override
                    public void onEvent(int code, boolean isValidate) {
                        slvLoading.hide();
                        dismiss();
                        if (pEvent != null) pEvent.onEvent(code, isValidate);
                    }
                });
            }
        });
        lvContent.setAdapter(adapter);
    }

    public void show(PhotoEvent event) {
        if (isShowing()) return;
        this.pEvent = event;
        slvLoading.loading("");
        adapter.clear();
        adapter.add(PhotoFileHelper.getInstance(curFileName).getFileInfos());
        if (contain != null && contain.get() != null) {
            showAtLocation(contain.get(), Gravity.BOTTOM, 0, DisplayUtils.dip2px(context.get(), 50));
            slvLoading.hide();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public void release() {
        if (adapter != null) adapter.clear();
        if (context != null) {
            context.clear();
            context = null;
        }
        if (contain != null) {
            contain.clear();
            contain = null;
        }
        rootView = null;
    }
}
